//添加finally：因为还有一个参数里面还有一个complete方法。
 //const baseUrl = 'http://localhost:8081'
const baseUrl = 'http://192.168.0.151:8081'
Promise.prototype.finally = function (callback) {
  let P = this.constructor;
  return this.then(
    value => P.resolve(callback()).then(() => value),
    reason => P.resolve(callback()).then(() => { throw reason })
  );
};

//封装异步api
const wxPromisify = fn => {
  return function (obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function (res) {
        resolve(res)
      }

      obj.fail = function (res) {
        reject(res)
      }

      fn(obj)
    })
  }
}

const getLocationPromisified = wxPromisify(wx.getLocation);//获取经纬度
const showModalPromisified = wxPromisify(wx.showModal);//弹窗

// 封装post请求
const post = (url, data) => {
  var promise = new Promise((resolve, reject) => {
    //网络请求
    wx.request({
      url: baseUrl+url,
      data: data,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        //'token': app.globalData.token
        'token': wx.getStorageSync('token')
      },
      success: function (res) {//服务器返回数据
        console.log("res.data:" + res.data)
        if (res.statusCode == 200 && res.data != 50000) {
          resolve(res);
        } else if (res.data == 50000 && res.statusCode == 200){
          console.log("登录身份过期，请重新登录！")
          wx.showModal({
            title: '提示',
            content: '登录身份过期，请重新登录！',
          })
          // wx.showToast({
          //   title: '登录身份过期，请重新登录！',
          // });
          wx.reLaunch({
            url: '../login/login',
          })
        }else {//返回错误提示信息
          reject(res.data);
          wx.showToast({
            title: '数据获取失败，请刷新',
          });
        }
      },
      error: function (e) {
        reject('error网络出错');
        wx.showToast({
          title: '数据获取失败，请刷新',
        });
      },
      fail: function(){
        reject('fail网络出错');
        wx.showToast({
          title: '数据获取失败，请刷新',
        });
      }
    })
  });
  return promise;
}
// 封装get请求
const get = (url, data) => {
  var promise = new Promise((resolve, reject) => {
    //网络请求
    wx.request({
      url: baseUrl +url,
      data: data,
      header: {
        'content-type': 'application/json',
        //'token': app.globalData.token
        'token': wx.getStorageSync('token')
      },
      success: function (res) {//服务器返回数据
        if (res.statusCode == 200 && res.data != 50000) {
          resolve(res);
        } else if (res.data == 50000 && res.statusCode == 200) {
         
          wx.showModal({
            title: '提示',
            content: '登录身份过期，请重新登录！',
          })
          console.log("登录身份过期，请重新登录！")
          wx.reLaunch({
            url: '../../../login/login',
          })
        } else {//返回错误提示信息
          reject(res.data);
        }
      },
      error: function (e) {
        reject('error网络出错');
        wx.showToast({
          title: '数据获取失败，请刷新',
        });
      },
      fail: function () {
        reject('fail网络出错');
        wx.showToast({
          title: '数据获取失败，请刷新',
        });
      }
    })
  });
  return promise;
}

module.exports = {
  post,
  get,
  getLocationPromisified,
  showModalPromisified
}